package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.business.dao.inter.DonationDao;
import com.yunfeisoft.business.dao.inter.DonationItemDao;
import com.yunfeisoft.business.model.Donation;
import com.yunfeisoft.business.service.inter.DonationService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.sql.separation.DataSourceChange;
import com.yunfeisoft.common.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * ClassName: DonationServiceImpl
 * Description: 捐赠物品登记service实现
 * Author: Jackie liu
 * Date: 2020-05-14
 */
@Service("donationService")
public class DonationServiceImpl extends BaseServiceImpl<Donation, String, DonationDao> implements DonationService {

    @Autowired
    private DonationItemDao donationItemDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<Donation> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public int save(Donation donation) {
        if (!CollectionUtils.isEmpty(donation.getDonationItemList())) {
            donationItemDao.batchInsert(donation.getDonationItemList());
        }
        return super.save(donation);
    }

    @Override
    public int modify(Donation donation) {
        donationItemDao.deleteByDonationId(donation.getId());
        if (!CollectionUtils.isEmpty(donation.getDonationItemList())) {
            donationItemDao.batchInsert(donation.getDonationItemList());
        }
        return super.modify(donation);
    }
}